package com.myapp.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.myapp.R;
import com.myapp.utils.ClickUtils;
import com.myapp.utils.PreferencesUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    public static final String LOGIN_NAME = "login_name";
    public static final String LOGIN_PSW = "login_psw";
    public static final String UID="uid";
    public static final String TOKEN="token";

    private String name;
    private String psw;

    @BindView(R.id.tv_title_login)
    TextView tvTitleLogin;
    @BindView(R.id.tv_title_register)
    TextView tvTitleRegister;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.iv_login_confirm)
    ImageView ivLoginConfirm;
    @BindView(R.id.tv_forget_password)
    TextView tvForgetPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        name = PreferencesUtil.getInstance(getApplicationContext()).getString(LOGIN_NAME,null);
        psw = PreferencesUtil.getInstance(getApplicationContext()).getString(LOGIN_PSW,null);
        if(name!=null&psw!=null) {
            etPhone.setText(name);
            etPassword.setText(psw);
        }
    }


    @OnClick({R.id.iv_login_confirm,R.id.tv_title_register,R.id.tv_forget_password})
    void onClick(View view){
        switch (view.getId()){
            case R.id.tv_title_register:
                if (ClickUtils.isFastDoubleClick()) {
                    return;
                }
                Intent intent = new Intent(this,RegisterActivity.class);
                startActivity(intent);
                break;

            case R.id.iv_login_confirm:
                if (ClickUtils.isFastDoubleClick()) {
                    return;
                }
                String inputName = etPhone.getText().toString();
                String inputPassword = etPassword.getText().toString();
                PreferencesUtil.getInstance(getApplicationContext()).putString(LOGIN_NAME,inputName);
                PreferencesUtil.getInstance(getApplicationContext()).putString(LOGIN_PSW,inputPassword);
                GizWifiSDK.sharedInstance().setListener(mListener);
                GizWifiSDK.sharedInstance().userLogin(inputName, inputPassword);
                break;

            case R.id.tv_forget_password:
                if (ClickUtils.isFastDoubleClick()) {
                    return;
                }
                Intent intent1 = new Intent(this,ForgetPasswordActivity.class);
                startActivity(intent1);
                break;

        }
    }

    // 实现回调
    GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public  void didUserLogin(GizWifiErrorCode result, String uid, String token) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                Toast.makeText(getApplicationContext(),"登陆成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                // 登录成功
            } else {
                Toast.makeText(getApplicationContext(),"登陆失败",Toast.LENGTH_SHORT).show();
                // 登录失败

            }
            PreferencesUtil.getInstance(getApplicationContext()).putString(UID,uid);
            PreferencesUtil.getInstance(getApplicationContext()).putString(TOKEN,token);
        }


    };
}
